package com.ericsson.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormulaConvertImsFive {

    /**第一步处理
     * (@AccessNetPcscf)  ->  AccessNetPcscf
     * 数组5个元素
     * 第一个 正则匹配 \(@[a-zA-Z]*\) 查找如 (@AccessNetPcscf)
     * 第二个元素(@替换空第三个元素空
     * 第四个元素）替换成第五元素空
     * __ROUND_BRACKETS__ 用于获取(@AccessNetPcscf)数据标识
     * 结果是sum{sbgSipIncSessDuration}(@AccessNetPcscf) -> 结果是sum{sbgSipIncSessDuration}__ROUND_BRACKETS__AccessNetPcscf
     */
    public static final String __ROUND_BRACKETS__ = "__N1__";
    public static final String[] ROUND_BRACKETS = {"\\(@[a-zA-Z]*\\)","(@",__ROUND_BRACKETS__,")",""};

    /**第二步处理
     * sum{sbgSipIncSessDuration} ->  sbgSipIncSessDuration
     * 数组5个元素
     * 第一个 正则匹配 \(@[a-zA-Z]*\) 查找如 (@AccessNetPcscf)
     * 第二个元素(@替换空第三个元素空
     * 第四个元素）替换成第五元素空
     * 结果 sum{sbgSipSessionOutNormalRelease}(@AccessNetPcscf)+ sum{sbgSipSessionOutNormalRelease}(@CoreNetPcscf)
     * ->  sbgSipSessionOutNormalRelease__ROUND_BRACKETS__AccessNetPcscf+ sbgSipSessionOutNormalRelease__ROUND_BRACKETS__CoreNetPcscf
     */
    public static final String[]  SUM_BRACKETS= {"sum\\{([^}])[a-zA-Z]*\\}","sum{","","}",""};


    /**第三步处理
     * sbgSipNetIncSessionEstabNetworkSuccessATPerPlmn.{access-type = 5G} ->
     *
     *
     * 数组5个元素
     * 第一个 正则匹配 \(@[a-zA-Z]*\) 查找如 (@AccessNetPcscf)
     * 第二个元素(@替换空第三个元素空
     * 第四个元素）替换成第五元素空
     *
     * 把
     */
    //等号
    public static final String[] EQUAL = {"=","__N2__"};
    //减号
    public static final String[] MINUS = {"-","__N3__"};
    public static final String __POINT__ATTR__ = "__N4__";
    public static final String __POINT__CHAR__ = "__N6__";
    public static final String[] OR = {"|","__N5__"};;
    public static final String[]  POINT_BRACKETS= {"\\.\\{([^}])*\\}",".{",__POINT__ATTR__,"}",""};
    public static final String[]  CHARACTERPOINT= {"[A-Za-z]\\.",".",__POINT__CHAR__,"}"};


    /**
     * 第四步处理 更换大括号变成{} -> ()
     */
    public static final String[]  BRACKETSwww= {"sum\\{([^}])[a-zA-Z]*\\}","sum{","","}",""};

    private static String convertFormula1(String formulaText, String[] convert) {
        //第一步 替换(@AccessNetPcscf)
        Pattern p = Pattern.compile(convert[0]);
        Matcher m = p.matcher(formulaText);
        while (m.find()) {
            String tmp = m.group();
            String target = tmp.replace(convert[1], convert[2])
                    .replace(convert[3], convert[4]);
            formulaText = formulaText.replace(tmp, target);
        }
        return formulaText;
    }

    private static String convertFormula2(String formulaText, String[] convert, String[] equal, String[] minus,String or[]) {
        //第一步 替换(@AccessNetPcscf)
        Pattern p = Pattern.compile(convert[0]);
        Matcher m = p.matcher(formulaText);
        while (m.find()) {
            String tmp = m.group();
            String target = tmp.replace(convert[1], convert[2]).
                    replace(convert[3], convert[4]).
                    replace(equal[0], equal[1]).
                    replace(minus[0], minus[1]).
                    replace(or[0], or[1]);
            formulaText = formulaText.replace(tmp, target);
        }
        return formulaText;
    }

    private static String convertFormula3(String formulaText, String[] convert) {
        Pattern p = Pattern.compile(convert[0]);
        Matcher m = p.matcher(formulaText);
        while (m.find()) {
            String tmp = m.group();
            String target = tmp.replace(convert[1], convert[2]);
            formulaText = formulaText.replace(tmp, target);
        }
        return formulaText;
    }

    public static String getResult(String formulaText){
        //第一步
        formulaText = convertFormula1(formulaText, FormulaConvertImsFive.ROUND_BRACKETS);
        //第二步
        formulaText = convertFormula1(formulaText, FormulaConvertImsFive.SUM_BRACKETS);
        //第三步
        formulaText = convertFormula2(formulaText, FormulaConvertImsFive.POINT_BRACKETS,
                FormulaConvertImsFive.EQUAL, FormulaConvertImsFive.MINUS,FormulaConvertImsFive.OR);
        formulaText =convertFormula3(formulaText,FormulaConvertImsFive.CHARACTERPOINT);
        //最后一步
        formulaText =formulaText.replace("{","(").replace("}",")");
        return formulaText;
    }

    public static String restoreFormula(String var) {
        return var.replace("__N1__",".").replace("__N2__","=").replace("__N3__","-")
                .replace("__N4__",".").replace("__N5__","|");
    }
}
